package com.longnuoqi.mydoctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;


import com.longnuoqi.mydoctor.fragment.NearbyStoreFragment;
import com.longnuoqi.mydoctor.utils.ActivityCollection;
import com.longnuoqi.mydoctor.utils.OtherUtil;
import com.yunhe.mydoctor.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Lenovo on 2016/1/5.
 */
public class NearbyPlaceActivity extends AppCompatActivity {
    private ImageView store;
    private ImageView hospital;
    private RelativeLayout map;

    private double longitude;//经度
    private double latitude;//纬度
    private static boolean isExit = false;//判断是否退出
    private static boolean hasTask = false;//判断是否是任务
    private Timer timer = new Timer();//声明timer对象，计时器
    private TimerTask timerTask = new TimerTask() {//为timer安排一次执行或重复执行的任务
        @Override
        public void run() {
            isExit = false;
        }
    };

    /*双击退出程序*/
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(NearbyPlaceActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                if (!hasTask) {
                    timer.schedule(timerTask, 2000);//安排在指定延迟后执行指定的任务
                }
            } else {
                ActivityCollection.finishAll();
                //回收资源
                System.exit(0);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nearby_place_layout);
        ActivityCollection.addActivity(this);
        OtherUtil.showProgress(this, "", "正在加载列表信息……", 4000);
        Intent intent = getIntent();
        latitude = intent.getDoubleExtra("latitude", 0);
        longitude = intent.getDoubleExtra("longitude", 0);
        initView();
    }

    private void initView() {
        store = (ImageView) findViewById(R.id.nearby_top_store);
        hospital = (ImageView) findViewById(R.id.nearby_top_hospital);
        map = (RelativeLayout) findViewById(R.id.nearby_top_map_rl);
        store.setOnClickListener(onClickListener);
        hospital.setOnClickListener(onClickListener);
        map.setOnClickListener(onClickListener);

        store.setSelected(true);
        hospital.setSelected(false);
        showStore(new NearbyStoreFragment());
    }

    private void showStore(Fragment fragment) {
        Bundle bundle = new Bundle();
        bundle.putDouble("longitude", longitude);
        bundle.putDouble("latitude", latitude);

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        fragment.setArguments(bundle);
        transaction.replace(R.id.nearby_framelayout, fragment);
        transaction.commit();
    }


    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.nearby_top_store:
                    store.setSelected(true);
                    hospital.setSelected(false);
                    showStore(new NearbyStoreFragment());
                    break;
                case R.id.nearby_top_hospital:
                    store.setSelected(false);
                    hospital.setSelected(true);
                    showStore(new NearbyStoreFragment());
                    break;
                case R.id.nearby_top_map_rl:
                    Intent intent = getIntent();
                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollection.removeActivity(this);
    }

}
